using System;
using System.Windows.Forms;
using System.Data;

namespace ERP.Classes
{
	/// <summary>
	/// Summary description for csComboBox.
	/// </summary>
	public class csComboBox
	{
		private DataSet m_DataSet;
		private string fieldText;
		private string fieldValue;

		public csComboBox()
		{
			//
			// TODO: Add constructor logic here
			//
			this.fieldText="";
			this.fieldValue="";
			this.m_DataSet=new DataSet("myDataSet");
		}

		public csComboBox(string fieldText,string fieldValue)
		{
			//
			// TODO: Add constructor logic here
			//
			this.fieldText=fieldText;
			this.fieldValue=fieldValue;
		}

		public void KetDuLieu(ComboBox myCombo,string tableName,string condition)
		{
			try
			{
				
				csCSDL csdl=new csCSDL();
				//this.m_DataSet.Clear();
				this.m_DataSet=csdl.OpenTable(tableName,condition);

				if (this.m_DataSet==null)
				{
					myCombo.Enabled=false;
					return;
				}
				myCombo.DataSource=this.m_DataSet.Tables[0];
				myCombo.DisplayMember=this.fieldText;
				myCombo.ValueMember=this.fieldValue;
				//myCombo.Refresh();
				myCombo.SelectedIndex=0;
			}
			catch
			{
				//myCombo.Enabled = false;
				//myCombo.Text="";
			}
		}
		public void ShowDatabySQL(ComboBox myCombo,string sql)
		{
			try
			{
				myCombo.Items.Clear();
				csCSDL csdl=new csCSDL();
				this.m_DataSet=csdl.SelecSQL(sql);

				if (this.m_DataSet==null)
				{
					myCombo.Enabled=false;
					return;
				}
				myCombo.DataSource=this.m_DataSet.Tables[0];
				myCombo.ValueMember=this.fieldValue;
				myCombo.DisplayMember=this.fieldText;
				//myCombo.Refresh();
				//myCombo.SelectedIndex=0;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		public void KetDuLieu(ComboBox myCombo,string condition)
		{
			try
			{
				myCombo.Items.Clear();
				csCSDL csdl=new csCSDL();
				this.m_DataSet=csdl.SelecSQL(condition);

				if (this.m_DataSet==null)
				{
					myCombo.Enabled=false;
					return;
				}
				myCombo.DataSource=this.m_DataSet.Tables[0];
				myCombo.ValueMember=this.fieldValue;
				myCombo.DisplayMember=this.fieldText;
				//myCombo.Refresh();
				//myCombo.SelectedIndex=0;
			}
			catch(Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}
		public int Selected(ComboBox myCombo)
		{
			try
			{
				return myCombo.SelectedIndex;
			}
			catch
			{
				return -1;
			}
		}

	}
}
